//
//  CPSCHomeToolsAllViewController.m
//  CollTest
//
//  Created by codew on 9/13/21.
//

#import "CPSCHomeToolsAllViewController.h"
#import "CPSCHomeToolsAllTBCell.h"
#import "CPSCHomeToolsConstantsValue.h"

@interface CPSCHomeToolsAllViewController ()<UITableViewDelegate, UITableViewDataSource>


@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray * dataArray;
@property (nonatomic, assign) NSInteger page;

@end

static NSString * CPSCHomeToolsAllTBCellIdentifier = @"CPSCHomeToolsAllTBCell";
@implementation CPSCHomeToolsAllViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initSubviews];
    [self makeContraints];
}

- (void)initSubviews
{
    [self.view addSubview:self.tableView];
    
    [self deBugUI];
}

- (void)makeContraints {
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

- (void)deBugUI
{
    for (int i = 0; i < 30; i++) {
        
        int valueRow = arc4random_uniform(10);
        
        while (valueRow == 0 ) {
            valueRow = arc4random_uniform(10);
        }
        
        NSNumber * ob1 = [[NSNumber alloc] initWithInt:valueRow];
        
        [self.dataArray addObject:ob1];
    }
    
    [self.tableView reloadData];
}


#pragma mark - Getter Setter
- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        
        _dataArray = [NSMutableArray array];
    }
    
    return _dataArray;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [UITableView new];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionHeaderHeight = 0;
        _tableView.sectionFooterHeight = 0;
        _tableView.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
         
    }
    
    return _tableView;
}
#pragma mark - <UITableViewDelegate, UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSNumber *item = self.dataArray[indexPath.row];
    int valueRow = item.intValue;
    
    CPSCHomeToolsAllTBCell  * cell = [tableView dequeueReusableCellWithIdentifier:CPSCHomeToolsAllTBCellIdentifier];
    
    if (!cell) {
        
        [tableView registerNib:[UINib nibWithNibName:CPSCHomeToolsAllTBCellIdentifier bundle:nil] forCellReuseIdentifier:CPSCHomeToolsAllTBCellIdentifier];
        
        cell = [tableView dequeueReusableCellWithIdentifier:CPSCHomeToolsAllTBCellIdentifier];
    }
    
    cell.backgroundColor = kCPSCRandomColor;
    [cell updateCellWithItem:valueRow];
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSNumber *item = self.dataArray[indexPath.row];
    int valueRow = item.intValue;
    
    CGFloat heightTop = CPSCHomeToolsTBCellTopViewHeight;
    CGFloat heightBottomContent;
    
    if ( valueRow%2 == 0 ) {
        
        heightBottomContent = valueRow / 2 * CPSCHomeToolsTBCellItemViewHeight;
    }else{
        
        heightBottomContent = (valueRow / 2 + 1) * CPSCHomeToolsTBCellItemViewHeight;
    }
    
    CGFloat height = heightTop + heightBottomContent;
    return height;
}

@end
